let postCatagory = document.getElementById("postCatagory")
let postTitle = document.getElementById("postTitle")
let postDescription = document.getElementById("postDescription")
let postDueDate = document.getElementById("postDueDate")
let marks = document.getElementById("marks")

window.addEventListener("load",()=>{
    document.getElementById("managerLoginNumberAndName").innerText=`Manager ID:${Cookies.get('id')} Name:${Cookies.get('username')}`
    let date = new Date();
    postDueDate.value =  `${date.getFullYear()} ${date.getMonth()+1} ${date.getDate()+1} 00:00:00`;
    $.post("/manager/selectAllTasks", null, (res) => {
        if (res.status == "3001") {
            alert(res.message)
            document.location = "/public/html/home.html"
        } else if (res.data) {


            let allTasks = document.querySelector("#allTasks>tbody");

            for (let resArrayElement of res.data) {
                let tr = document.createElement("tr")
                let rowID
                for (let objKeyAndValue in resArrayElement) {
                       if (objKeyAndValue == "id") {
                         rowID=resArrayElement[`${objKeyAndValue}`]
                       }
                    let td = document.createElement("td")
                    if (objKeyAndValue == "due_date") {
                        let timeBigInt = resArrayElement[`${objKeyAndValue}`]*1000
                        let date= new Date(timeBigInt)

                        td.innerText =`${date.toLocaleString()}`
                    } else {
                        td.innerText = resArrayElement[`${objKeyAndValue}`]
                    }

                    tr.appendChild(td)

                }
                let tdOpration = document.createElement("td")
                let button = document.createElement("button")
                button.setAttribute("type","button")
                button.setAttribute("data-id",rowID)

                button.innerText="DELETE"
                button.classList.add("button")
                button.classList.add("btn-danger")
                button.addEventListener("click",(event)=>{
                    let deleteKey = event.target.getAttribute("data-id")
                    $.post("/manager/deleteATask", {"id":deleteKey}, (res) => {
                    if (res.status == "3001") {
                        alert(res.message)
                        document.location = "/public/html/home.html"
                    } else  {
                        alert(res.message)
                        location.reload()

                    }

                },"json")});
                tdOpration.appendChild(button)
                tr.appendChild(tdOpration)

                allTasks.appendChild(tr)
            }
        } else {
            let today_tasks_table = document.querySelector("#today_tasks_table>tbody");
            let tr = document.createElement("tr")
            let td = document.createElement("td")
            td.innerText = "No Today Tasks"
            tr.appendChild(td)
            today_tasks_table.appendChild(tr)

        }

    }, "json")


})


document.getElementById("postNewTask").addEventListener("click",(event)=>{
    let date = Math.round(new Date(postDueDate.value).getTime() / 1000)


    let parameter = {
        "title": postTitle.value,
        "catagory": postCatagory.value,
        "description": postDescription.value,
        "due_date": date,
        "marks": marks.value
    }
    $.post("/manager/createNewTask",parameter, function (res) {
        if (res.status == "3001") {
            alert(res.message)
            document.location = "/public/html/home.html"
        }
        else {
            document.location.reload()
            alert(res.message)
        }

    },"json")


})